<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Modules\DataCheck\Enums\TaskSourceFileEnum;
use Modules\DataCheck\Excel\Import\SourceFileImport;
use Modules\DataCheck\Models\DcTaskSourceFile;
use Modules\DataCheck\Services\Import\SourceFileImportService;

class ImportSourceFile extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'import:source-file {fileId} {--force}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '导入原始数据文件';

    /**
     * Execute the console command.
     */
    public function handle(SourceFileImport $import): int
    {
        $fileId = $this->argument('fileId');
        $force = $this->option('force');

        $sourceFile = DcTaskSourceFile::query()->find($fileId);
        if (empty($sourceFile)) {
            $this->error('导入失败，导入的原始数据文件不存在');
            return Command::FAILURE;
        }

        if ($sourceFile->file_state !== TaskSourceFileEnum::WAIT->value && !$force) {
            $this->error('导入失败，原始数据文件已导入或正在导入');
            return Command::FAILURE;
        }

        //导入
        $import->handle($sourceFile, $force);

        return Command::SUCCESS;
    }
}
